package it.unicam.cs.cosy.bioshape.core.components;

/**
 * Manages completely diffused concentrations.
 * 
 * @author Alanis Gaston
 */
public class ConcentrationGroupedEntity {
	// VALORE DELLA CONCENTRAZIONE
	private double concentration;
	// INDICA IL VALORE DI SATURAZIONE
	private double saturation;
	// INDICA IL NOME DELLA CONCENTRAZIONE
	private String name;

	/**
	 * A ConcentrationGroupedEntity.
	 * 
	 * @param concentration
	 * @param saturation
	 * @param name
	 */
	public ConcentrationGroupedEntity(double concentration, double saturation,
			String name) {
		this.concentration = concentration;
		this.saturation = saturation;
		this.name = name;
	}

	/**
	 * 
	 * @return The name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 
	 * @return the concentration value
	 */
	public double getConcentration() {
		return concentration;
	}

	/**
	 * 
	 * @return the saturation value
	 */
	public double getSaturation() {
		return saturation;
	}

	/**
	 * Sets the name
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Sets the concentration level
	 * 
	 * @param value
	 * 
	 * @return false the concentration value is higher than the saturation
	 *         value.
	 */
	public boolean setConcentration(double value) {
		if (value > saturation)
			return false;
		concentration = value;
		return true;
	}

	/**
	 * Sets the saturation value
	 * 
	 * @param value
	 */
	public void setSaturation(double value) {
		saturation = value;
	}

	/**
	 * Increments the concentration value
	 * 
	 * @param value
	 * 
	 * @return false if the input value plus the previous concentration value is
	 *         higher than the saturation value.
	 */
	public boolean addConcentration(double value) {
		value = value + concentration;
		if (value > saturation)
			return false;
		concentration = value;
		return true;
	}

	/**
	 * Checks if a certain concentration quantity is available.
	 * 
	 * @param value
	 * 
	 * @return false if the desired concentration quantity is not available.
	 */
	public boolean isAvailable(double value) {
		if ((Math.random() * saturation) > concentration
				|| value > concentration)
			return false;
		return true;
	}

	/**
	 * Decrements the concentration value.
	 * 
	 * @param value
	 * 
	 * @return false if the previous concentration minus the input value is
	 *         lower than 0.
	 */
	public boolean subtract(double value) {
		if (!this.isAvailable(value))
			return false;
		concentration = concentration - value;
		return true;
	}

}
